package com.hexacta.hrs.repositories.impl;

import java.util.Collection;
import java.util.Hashtable;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.util.Assert;
import org.vectrics.common.db.RecordObject;

import com.hexacta.hrs.repositories.RecordObjectRepository;

/**
 * {@link RecordObjectRepository} de prueba que usa un {@link Hashtable} como base de datos.
 * 
 * Created on 19/03/2009
 * 
 * @author pgarcia
 * 
 * @param <T>
 *            el tipo de entidad del {@link RecordObjectRepository}.
 * 
 */
public class MemoryRecordObjectRepository<T extends RecordObject> implements RecordObjectRepository<T> {

    private Hashtable<Long, T> cache = new Hashtable<Long, T>();

    /** {@inheritDoc} */
    public T forId(Long id) {
        T entity = null;
        if (cache.containsKey(id)) {
            entity = cache.get(id);
        }
        return entity;
    }

    /** {@inheritDoc} */
    public void save(T entity) {
        Assert.notNull(entity);
        cache.put(entity.getId(), entity);
    }

    /** {@inheritDoc} */
    public void delete(T entity) {
        Assert.notNull(entity);
        if (cache.containsKey(entity.getId())) {
            cache.remove(entity.getId());
        }
    }

    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    public Collection<T> matching(T criteria) {
        return CollectionUtils.EMPTY_COLLECTION;
    }

    /** {@inheritDoc} */
    public Collection<T> all() {
        return cache.values();
    }
}
